package de.htw.praxisprojekt.actions;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.List;

import javax.swing.JFileChooser;

import de.htw.praxisprojekt.AlgoData;
import de.htw.praxisprojekt.LineAlgo;
import de.htw.praxisprojekt.LoadSaveAlgoPoints;
import de.htw.praxisprojekt.data.AlgoPoint;
import de.htw.praxisprojekt.data.LineFileFilter;

public class LoadAction implements ActionListener{

	private LineAlgo lineAlgo;

	public LoadAction(LineAlgo lineAlgo){
		this.lineAlgo=lineAlgo;
	}

	public void actionPerformed(ActionEvent arg0){
		JFileChooser fileChooser=new JFileChooser(".");
		fileChooser.setFileFilter(new LineFileFilter());
		fileChooser.setAcceptAllFileFilterUsed(false);
		fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
		fileChooser.showOpenDialog((Component)arg0.getSource());
		File selectedFile=fileChooser.getSelectedFile();
		if(selectedFile!=null){
			List<AlgoPoint> loadAlgoPoints=LoadSaveAlgoPoints.loadAlgoPoints(selectedFile);
			AlgoData algoData=lineAlgo.getAlgoData();
			algoData.setScaleFactor(1);
			if(algoData.isScaleLoad()){
				scalePoints(algoData,loadAlgoPoints);
			}
			if(algoData.isCenterLoad()){
				centerPoints(loadAlgoPoints);
			}
			lineAlgo.getAlgoData().getAlgoPoints().clear();
			lineAlgo.getAlgoData().getAlgoPoints().addAll(loadAlgoPoints);
			// lineAlgo.getAlgoData().setScaleFactor()
		}
		lineAlgo.redrawBasic();
		lineAlgo.changeComponentBehavior();
	}

	private void centerPoints(List<AlgoPoint> algoPoints){
		float minX=Float.MAX_VALUE;
		float minY=Float.MAX_VALUE;
		float maxX=0;
		float maxY=0;
		for(int i=0;i<algoPoints.size();++i){
			float x=algoPoints.get(i).getPoint().x;
			float y=algoPoints.get(i).getPoint().y;
			if(x>maxX)
				maxX=x;
			if(y>maxY)
				maxY=y;
			if(x<minX)
				minX=x;
			if(y<minY)
				minY=y;
		}
		float addX=lineAlgo.getDrawPanel().getBounds().width/2-(minX+maxX)/2;
		float addY=lineAlgo.getDrawPanel().getBounds().height/2-(minY+maxY)/2;
		for(int i=0;i<algoPoints.size();++i)
			algoPoints.get(i).getPoint().add(addX,addY);
	}

	private void scalePoints(AlgoData algoData,List<AlgoPoint> algoPoints){
		float minX=Float.MAX_VALUE;
		float minY=Float.MAX_VALUE;
		float maxX=0;
		float maxY=0;
		for(int i=0;i<algoPoints.size();++i){
			float x=algoPoints.get(i).getPoint().x;
			float y=algoPoints.get(i).getPoint().y;
			if(x>maxX)
				maxX=x;
			if(y>maxY)
				maxY=y;
			if(x<minX)
				minX=x;
			if(y<minY)
				minY=y;
		}
		int panelWidth=lineAlgo.getDrawPanel().getBounds().width;
		int panelHeight=lineAlgo.getDrawPanel().getBounds().height;
		float diffX=maxX-minX;
		float diffY=maxY-minY;
		float scalar=1.0f;

		float x=panelWidth/diffX;
		float y=panelHeight/diffY;
		if(x<y)
			scalar=x*0.8f;
		else
			scalar=y*0.8f;
		if(diffX>panelWidth){
			scalar=(panelWidth/diffX)*0.8f;
		}
		if(diffY>panelHeight){
			float scalarHeight=(panelWidth/diffX)*0.8f;
			if(scalarHeight<scalar)
				scalar=scalarHeight;
		}
		algoData.setScaleFactor(scalar);
		for(int i=0;i<algoPoints.size();++i)
			algoPoints.get(i).getPoint().mul(scalar);
	}
}